import Authorization.User;
import Authorization.UserController;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.*;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 15.11.13
 */
public class TestUserController {

    @Test
    public void testHelloUserRegistration() throws IOException {
        int millis = (int)System.currentTimeMillis() % 1000;
        String userInput = "1\n"+millis+"1\n111";
        ByteArrayInputStream in = new ByteArrayInputStream(userInput.getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        User newUser = controller.helloUser();
        assertNotNull(newUser);
       }

    @Test
    public void testHelloUserLogin() throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream("2\n111\n111".getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        User newUser1 = controller.helloUser();
        assertNotNull(newUser1);
    }

    @Test
    public void testHelloUserNull() throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream("3".getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        assertNull(controller.helloUser());
    }

    @Test
    public void testCreateUser() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
        int millis = (int)System.currentTimeMillis() % 1000;
        String log = String.valueOf(millis);
        User user = new User(log,"pass");
        Method createUserMethod = UserController.class.getDeclaredMethod("createUser");
        createUserMethod.setAccessible(true);
        String toByte = log+"\npass";
        ByteArrayInputStream in = new ByteArrayInputStream(toByte.getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        assertTrue(user.equals(createUserMethod.invoke(controller)));
    }

    @Test
    public void testGetValidPass() throws NoSuchMethodException, IOException, InvocationTargetException, IllegalAccessException {
        int millis = (int)System.currentTimeMillis() % 1000;
        Method getValidPass = UserController.class.getDeclaredMethod("getValidPass");
        getValidPass.setAccessible(true);
        String userInput = String.valueOf(millis);
        ByteArrayInputStream in = new ByteArrayInputStream(userInput.getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        assertEquals(userInput,getValidPass.invoke(controller));
    }

    @Test
    public void testGetValidLogin() throws NoSuchMethodException, IOException, InvocationTargetException, IllegalAccessException {
        int millis = (int)System.currentTimeMillis()%1000;
        Method getValidLog = UserController.class.getDeclaredMethod("getValidLogin");
        getValidLog.setAccessible(true);
        String userInput = String.valueOf(millis);
        ByteArrayInputStream in = new ByteArrayInputStream(userInput.getBytes());
        System.setIn(in);
        UserController controller = new UserController();
        assertEquals(userInput,getValidLog.invoke(controller));
    }

    @Test
    public void testLoginUser() throws NoSuchMethodException, IOException, InvocationTargetException, IllegalAccessException {
        User mustBeLogged = new User("log","pass");
        Method loginUser = UserController.class.getDeclaredMethod("loginUser");
        loginUser.setAccessible(true);
        String userInput = "log\npass";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(userInput.getBytes());
        System.setIn(inputStream);
        UserController controller = new UserController();
        assertEquals(mustBeLogged,loginUser.invoke(controller));
    }


}
